package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

/**
 * 创建bean对象后，spring会自动在MQ中创建相应的交换机、队列、绑定关系，前提是MQ中原来不存在
 * 注意：spring在RabbitMQ中自动bean对应交换机、队列、绑定关系等，是在第一次连接尝试监听时，因此必须有@RabbitListener
 */
@Configuration
public class MQConfig {

    //@RabbitListener(queues = "order.release.order.queue")
//    public void testListen(OrderEntity orderEntity, Channel channel,Message message) throws IOException {
//        Date now = new Date();
//        System.out.println("发出消息到收到消息用的毫秒数："+(now.getTime()-orderEntity.getModifyTime().getTime()));
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//    }

    @Bean
    public Queue orderDelayQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange","order-event-exchange");
        map.put("x-dead-letter-routing-key","order.release.order");
        map.put("x-message-ttl",60000);//单位：毫秒
        return new Queue("order.delay.queue",true,false,false,map);
    }

    @Bean
    public Queue orderReleaseOrderQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        HashMap<String, Object> map = new HashMap<>();
        return new Queue("order.release.order.queue",true,false,false,map);
    }

    @Bean
    public Exchange orderEventExchange(){
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange("order-event-exchange",true,false,null);
    }

    @Bean
    public Binding orderCreateOrderBinding(){
        return new Binding("order.delay.queue", Binding.DestinationType.QUEUE,"order-event-exchange",
                "order.create.order",null);
    }

    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding("order.release.order.queue", Binding.DestinationType.QUEUE,"order-event-exchange",
                "order.release.order",null);
    }

    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE,"order-event-exchange",
                "order.release.other.#",null);
    }

    @Bean
    public Queue seckillQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        HashMap<String, Object> map = new HashMap<>();
        return new Queue("order.seckill.order.queue",true,false,false,map);
    }

    @Bean
    public Binding seckillBinding(){
        return new Binding("order.seckill.order.queue", Binding.DestinationType.QUEUE,
                "order-event-exchange","order.seckill.order",null);
    }

}
